import EventDispatcher from "./EventDispatcher";
import TSMap from "../struct/TSMap";


export default class EventProxy {


    private dispatcher: EventDispatcher;
    private eventMap: TSMap<string, any>;
    constructor() {
        this.eventMap = new TSMap();
    }
    setDispatcher(dispatcher: EventDispatcher) {
        this.dispatcher = dispatcher;
    }
    on(eventName, func: Function, target: Object) {
        if (!this.eventMap.get(eventName)) {
            this.dispatcher.on(eventName, func, target);
            this.eventMap.set(eventName, { eventName: eventName, target: target, func: func });
        }
    }
    has(eventName) {
        return this.eventMap.has(eventName);
    }
    offAll() {
        if (this.dispatcher) {
            this.eventMap.forEach((key, element) => {
                this.dispatcher.off(element.eventName, element.func, element.target);
            })
            this.eventMap.clear();
        }

    }
}
